<?php
/**
* 
*/
class Recruitment_model extends Model
{
	
	function Recruitment_model(){
		parent::Model();
	}

	function getAllRecruitment($condition = array(),$limit = array()){
		$this->db->from("job");
		//Check For Limit	
		if(is_array($limit) && count($limit) > 0)		
		{
			if(count($limit)==1)
	 			$this->db->limit($limit[0]);
			else if(count($limit)==2)
				$this->db->limit($limit[0],$limit[1]);
		}	
		$result = $this->db->get();

		return $result;
	}// End of getAllRecruitment function

	function getSingleRecruitment($condition = array(),$fields = ''){
		if(count($condition) > 0)
			$this->db->where($condition);

			$this->db->from("job");
			$this->db->join("users","job.user_id = users.id");
			$this->db->join("industry","industry.industry_id = job.industry_id");

			if($fields !='')
				$this->db->select($fields);
			else 		
		 		$this->db->select('job.job_id,job.industry_id,job.job_related,job.job_name,job.location,job.job_description,job.job_requirement,job.job_benefit,job.contact_info,job.email,job.phone,job.website,job.num_recruit,job.workplace,job.type_of_job,job.image,job.num_views,job.created_at,job.expired_at');
			 
			$result = $this->db->get();

			return $result;
	}

	function getJobRelated($condition = ""){
		if(!empty($condition))
			$condition = array("job.job_id"=> $condition);
			$this->db->where($condition);

			$this->db->from("job");
			$this->db->join("users","job.user_id = users.id");
			$this->db->join("industry","industry.industry_id = job.industry_id");
			$this->db->select("job.job_name,job.industry_id,job.location");
			$result = $this->db->get();
			return $result;
	}

	function view_counter($job_id = ""){
		if($job_id != ""){
			$this->db->where(array("job_id"=>$job_id));
			$this->db->from("job");
			$this->db->select("job.num_views");
			$result = $this->db->get();
			$result = $result->row_array();
			$num_views = $result['num_views'];
			if($num_views == 0){
				$num_views = 1;
				// insert vào trong CSDL
				$this->db->where(array("job_id"=>$job_id));
				$this->db->update("job",array("num_views"=>$num_views));
			}else{
				// update num_views cộng thêm 1
				$num_views = $num_views + 1;
				$this->db->where(array("job_id"=>$job_id));
				$this->db->update("job",array("num_views"=>$num_views));
			}
			return $num_views;

		}
	}

	function getAllIndustry(){
		$this->db->from("industry");
		$this->db->select("industry_id,industry_name,user_id");
		$result = $this->db->get();
		return $result;
	}

	function getNameCity(){
		//$this->db->where(array("location_id"=>$location_id));
		$this->db->from("location");
		$result = $this->db->get();
		return $result;
	}

	function getNameCityById($location_id){
		$this->db->where(array("location_id"=>$location_id));
		$this->db->from("location");
		$result = $this->db->get();
		return $result->row();
	}

	function getRecruitBySearch($condition = array(),$limit = array()){
		$this->db->from("job");

		//Check For Limit	
		if(is_array($limit) && count($limit) > 0)		
		{
			if(count($limit)==1)
	 			$this->db->limit($limit[0]);
			else if(count($limit)==2)
				$this->db->limit($limit[0],$limit[1]);
		}	

		if(is_array($condition) && count($condition) > 0)
			$this->db->where($condition);

			$result = $this->db->get();
		return $result;
	}

}

?>